/**
 * @Title: index.js
 * @Description: index
 * @author: Fanchao
 * @date: 2020/9/1 9:23 下午
 * @version V1.0
 */
import React from 'react';
import { Form, Input, Modal, message } from 'antd';
import { restUserPassword } from 'api/index';

const layout = {
    labelCol: { span: 4 },
    wrapperCol: { span: 20 },
};
const UpdatePassword = ({ visible, userId, cb }) => {
    const [form] = Form.useForm();
    const submitUpdatePass = async (values) => {
        const result = await restUserPassword(values);
        if (result.status === 'success') {
            message.success(`修改成功:新密码为 ${values.newPassword}`);
            form.resetFields();
            cb && cb(false);
        } else {
            message.error(result.message);
        }
    };
    return (
        <Modal
            title="修改密码"
            visible={visible}
            onOk={() => {
                form.validateFields().then(values => {
                    values.userId = userId;
                    submitUpdatePass(values);
                }).catch(info => {
                });
            }}
            onCancel={() => {
                form.resetFields();
                cb && cb(false);
            }}
        >
            <Form
                {...layout}
                layout="horizontal"
                form={form}
                name="basic"
                // 表单默认值，只有初始化以及重置时生效 莫用
                initialValues={{
                    // name: itemData.name
                }}
            >
                <Form.Item
                    label="新密码"
                    name="newPassword"
                    rules={[{ required: true, message: '必填' }]}
                >
                    <Input placeholder="请输入新密码"/>
                </Form.Item>
            </Form>
        </Modal>
    );
};

export default UpdatePassword;

